package com.itms.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itms.entity.CustomerProject;
import com.itms.entity.ErrorMsg;
import com.itms.entity.InterfaceLog;
import com.itms.entity.Project;
import com.itms.enums.TableName;
import com.itms.mapper.*;
import com.itms.pojo.Customer;
import com.itms.pojo.Loc;
import com.itms.pojo.TableDataSync;
import com.itms.service.BasicDataService;
import com.itms.service.LogService;
import com.itms.service.SycService;
import com.itms.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@EnableScheduling
@PropertySource("classpath:quartz.properties")
public class CustomerJob {
    @Reference
    private BasicDataService basicDataService;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private CustomerProjectMapper customerProjectMapper;

    @Autowired
    private LogService logService;

    @Autowired
    private SycService sycService;


    @Scheduled(initialDelayString = "${CustomerJob.initialDelayString}",fixedDelayString="${CustomerJob.fixedDelayString}" )
    public void deal(){

        try {
            List<ErrorMsg> errorList = new ArrayList<>();
            Date mdb = basicDataService.getTableSyncByTableName(TableName.MDBCustomer.toString());
            Date itms = this.sycService.getMaxSyncDate(TableName.MDBCustomer.toString());
            if (mdb==null) return;;
            if (itms!=null && TimeUtils.getDateSequence(mdb).equals( TimeUtils.getDateSequence(itms))) return;
            List<Customer> list =this.basicDataService.getCustomerList(itms);

            for (Customer c:list) {

                if(!"SGM".equals(c.getCustomerCode())){
                    continue;
                }
                Project project = null;
                boolean flag = false;
                    com.itms.entity.Customer customer = this.customerMapper.selectByCustomerCode(c.getCustomerCode(),null);
                    String projectCode = c.getProjectCode();
                    if (projectCode!=null){
                        flag =true ;
                        project = this.projectMapper.getProjectByProjectCode(projectCode,"1");
                    }
                    if (flag && project ==null){
                        this.logService.saveDetailLog(new ErrorMsg(c, projectCode + "项目代码不存在 "),TableName.MDBCustomer.toString(),this.getClass().getName(),c.getCustomerCode());
                        errorList.add(new ErrorMsg(c,projectCode+"项目代码不存在 "));
                        continue;
                    }
                    if (customer == null){
                        customer = new com.itms.entity.Customer();
                        customer.setCreateDate(new Date());
                        setCustomer(customer,c);
                        this.customerMapper.insertSelective(customer);
                    }else {
                        setCustomer(customer,c);
                        this.customerMapper.updateByPrimaryKeySelective(customer);
                    }
                if ((!flag )|| project == null )continue;
                CustomerProject cp = this.customerProjectMapper.findByProjectCustomerId(project.getId(),customer.getId());
                    if (cp == null) {
                        cp = new CustomerProject();
                        cp.setCustomerId(customer.getId());
                        cp.setProjectId(project.getId());
                        cp.setCreateDate(new Date());
                        cp.setCreateUserId(new BigDecimal(9999));
                        this.customerProjectMapper.insertSelective(cp);
                    }
            }
            if (errorList.size() == 0)
                this.sycService.save(mdb,TableName.MDBCustomer.toString());
            this.logService.saveErrorList(errorList,TableName.MDBCustomer.toString(),this.getClass().getName());
        }catch(Exception e){
            e.printStackTrace();
            this.logService.saveLog(e,TableName.MDBCustomer.toString(),this.getClass().getName());
        }
    }

    /**
     *
     * @param customer itms的客户
     * @param c 来自mdb的客户
     */
     void  setCustomer(com.itms.entity.Customer customer,Customer c){
        customer.setCustomerCode(c.getCustomerCode());
        customer.setCustomerName(c.getCustomerName());
        customer.setAddress(c.getAddress());
        customer.setIsValid(c.getIsValid());
        customer.setContactname(c.getUserName());
        customer.setTel(c.getTel());
//        customer.setCustomerType(c.getCustomerType());
        customer.setMobile(c.getMobile());
        customer.setLastUpdateDate(new Date());
        customer.setLastUpdateUserId(new BigDecimal(9999));
    }

}
